﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BitcoinTrading.Data
{
    public class DataMarket
    {
        public const string TABLE_KORBIT_ASK = "KORBIT_ASK";
        public const string TABLE_KORBIT_BID = "KORBIT_BID";
        public const string TABLE_KORBIT_OPEN = "KORBIT_OPEN";

        public const string TABLE_BTCE_ASK = "BTCE_ASK";
        public const string TABLE_BTCE_BID = "BTCE_BID";
        public const string TABLE_BTCE_OPEN = "BTCE_OPEN";

        public const string COLUMN_ORDER_ID = "ORDERID";
        public const string COLUMN_ORDER_TYPE = "ORDERTYPE";
        public const string COLUMN_BTC_PRICE = "BTCPRICE";
        public const string COLUMN_BTC_VOLUME = "BTCVOLUME";

        DataSet _ds;

        public DataMarket()
        {
            Initialized();
        }

        private void Initialized()
        {
            DataTable dt;

            _ds = new DataSet("MARKET_DATA");

            //KORBIT
            dt = TableAskBid(TABLE_KORBIT_ASK);
            _ds.Tables.Add(dt);

            dt = TableAskBid(TABLE_KORBIT_BID);
            _ds.Tables.Add(dt);

            dt = TableOpenOrder(TABLE_KORBIT_OPEN);
            _ds.Tables.Add(dt);
            
            //BTCe
            dt = TableAskBid(TABLE_BTCE_ASK);
            _ds.Tables.Add(dt);

            dt = TableAskBid(TABLE_BTCE_BID);
            _ds.Tables.Add(dt);

            dt = TableOpenOrder(TABLE_BTCE_OPEN);
            _ds.Tables.Add(dt);
        }

        private DataTable TableAskBid(string strTable)
        {
            DataTable oTable;
            DataColumn col;

            oTable = new DataTable(strTable);

            col = oTable.Columns.Add();
            col.ColumnName = COLUMN_BTC_PRICE;
            col.DataType = System.Type.GetType("System.Decimal");

            col = oTable.Columns.Add();
            col.ColumnName = COLUMN_BTC_VOLUME;
            col.DataType = System.Type.GetType("System.Decimal");

            return oTable;
        }

        private DataTable TableOpenOrder(string strTable)
        {
            //데이터 테이블 초기화
            DataColumn col;
            DataTable oTable;

            oTable = new DataTable(strTable);

            col = oTable.Columns.Add();
            col.ColumnName = COLUMN_ORDER_ID;
            col.DataType = System.Type.GetType("System.String");

            col = oTable.Columns.Add();
            col.ColumnName = COLUMN_ORDER_TYPE;
            col.DataType = System.Type.GetType("System.String");

            col = oTable.Columns.Add();
            col.ColumnName = COLUMN_BTC_PRICE;
            col.DataType = System.Type.GetType("System.Decimal");

            col = oTable.Columns.Add();
            col.ColumnName = COLUMN_BTC_VOLUME;
            col.DataType = System.Type.GetType("System.Decimal");

            return oTable;
        }

        public void SetDataTable(string strTableName, DataTable dt)
        {
            if (_ds.Tables.Contains(strTableName))
            {
                _ds.Tables[strTableName].Clear();
                _ds.Tables[strTableName].Merge(dt);
            }
        }

        public DataTable Tables(string strTableName)
        {
            DataTable objReturn = null;

            if (_ds.Tables.Contains(strTableName))
            {
                objReturn = _ds.Tables[strTableName];
            }

            return objReturn;
        }
    }
}
